#include "webwidget.h"
#include <iostream>
using namespace std;
WebWidget::WebWidget(QWidget *parent) : QWidget(parent)
{
    layout=new QHBoxLayout;
    layout->setMargin(0);
    setLayout(layout);
}

WebWidget::~WebWidget()
{
    delete layout;
}

WebWidget::WebPage *WebWidget::addpage(QUrl url)
{
    WebPage* newpage=new WebPage;
    newpage->page=new WebEngineView;
    newpage->page->setUrl(url);
    layout->addWidget(newpage->page);
    PageList.push_back(newpage);
    ShowPage(newpage);
    return newpage;
}
//通过Web控件新建页
WebWidget::WebPage *WebWidget::addpage(WebEngineView *page)
{
    WebPage* newpage=new WebPage;
    newpage->page=page;
    layout->addWidget(page);
    PageList.push_back(newpage);
    ShowPage(newpage);
    return newpage;
}


bool WebWidget::ShowPage(WebWidget::WebPage * webpage)
{
    if(!webpage){
        return false;
    }
    ;
    for(list<WebPage*>::iterator it=PageList.begin();it!=PageList.end();it++){
        if((*it)!=webpage){
            (*it)->page->hide();
        }
    }
    webpage->page->show();
    return true;
}

void WebWidget::delPage(WebWidget::WebPage *webpage)
{
    disconnect(webpage->page,nullptr,nullptr,nullptr);
    delete  webpage->page;
    delete  webpage;
//    webpage->page=new WebEngineView;

}
void WebWidget::delPage(int code)
{
    for(list<WebPage*>::iterator it=PageList.begin();it!=PageList.end();it++){
        if((*it)->code==code){
            delPage(*it);
            PageList.remove(*it);
            break;
        }
    }
}
//通过code索引page指针
WebWidget::WebPage *WebWidget::page(int code)
{
    for(list<WebPage*>::iterator it=PageList.begin();it!=PageList.end();it++){
        if((*it)->code==code){
            return (*it);
        }
    }
    return nullptr;
}

